@page "/Bill"
@attribute [UserNavigation("资金流水表")]
@rendermode InteractiveServer
@attribute [TabItemOption(Text = "资金流水表", Icon = "fa-solid fas fa-bars")]


<Table TItem="资金流水表" OnQueryAsync="@OnQueryAsync" IsPagination="true" IsStriped="true" IsBordered="true"
    ShowAddButton="false" ShowEditButton="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false"
    ShowDeleteButton="false" ShowToolbar="true"  ShowExtendButtons="true"    PageItemsSource="new int[] { 10, 25, 50, 100 }"
     ShowExportButton="true" @ref="@_tableRef" OnExportAsync="@OnExportAsync">
    <TableColumns>

        <TableColumn @bind-Field="@context.流水ID" Width="100"></TableColumn>
        <TableColumn @bind-Field="@context.用户ID" Searchable="true" Filterable="true" ></TableColumn>
        <TableColumn @bind-Field="@context.创建时间" Filterable="true" Sortable="true" DefaultSort="true"  DefaultSortOrder="@SortOrder.Desc"></TableColumn>
        <TableColumn @bind-Field="@context.变动金额" ></TableColumn>
        <TableColumn @bind-Field="@context.流水类型"   Filterable="true" ></TableColumn>
        <TableColumn @bind-Field="@context.交易状态"   Filterable="true" ></TableColumn>
        <TableColumn @bind-Field="@context.操作人ID" Searchable="true" Filterable="true"></TableColumn>
        <TableColumn @bind-Field="@context.关联业务ID"></TableColumn>
        <TableColumn @bind-Field="@context.备注"></TableColumn>
    </TableColumns>
    <RowButtonTemplate>
        <TableCellButton Color="Color.Primary" Icon="fa-solid fa-pen" Text="业务详情"
            OnClick="@(() => OnRowButtonClick(context))" />
    </RowButtonTemplate>
</Table>

@code {


    private Table<资金流水表>? _tableRef;
    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }
    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }

    [Inject]
    [NotNull]
    private ITableExport? TableExport { get; set; }
    [Inject]
    [NotNull]
    private MyIdentityContext AppContext { get; set; }
    [Inject]
    [NotNull]
    private IWorkOrderService WorkOrderService { get; set; }

      public DateTime 开始时间 { get; set; }

      public DateTime 结束时间 { get; set; }

    private async Task OnRowButtonClick(资金流水表 item)
    {
        try
        {

            if (item.关联业务ID == null)
            {
                await ToastService.Error("加载失败", "关联业务ID为空");
                return;
            }

            var option = new DialogOption()
            {
                Title = $"当前流水业务详情 - 业务ID: {item.关联业务ID}",
                Size = Size.ExtraLarge, // 建议调整对话框尺寸
                Component = BootstrapDynamicComponent.CreateComponent<TicketlistDetail>(new Dictionary<string, object?>
                {
                    [nameof(TicketlistDetail.TicketID)] = item.关联业务ID
                })
            };

            await DialogService.Show(option);
        }
        catch (Exception ex)
        {
            // 处理异常，例如显示错误提示
            await ToastService.Error("加载失败", ex.Message);
        }
    }
        private async Task<bool> OnExportAsync(ITableExportDataContext<资金流水表> context)
    {
         
         var ret=false;
        var option = new DialogOption()
            {
                Title = $"导出数据",
                Size = Size.ExtraLarge, // 建议调整对话框尺寸
                
                Component = BootstrapDynamicComponent.CreateComponent<DateTimeRange>(new Dictionary<string, object?>
                {
                    [nameof(DateTimeRange.OnConfirm)] = OnConfirm
                }),OnSaveAsync = async () =>
                {
                    if (开始时间 == DateTime.MinValue || 结束时间 == DateTime.MinValue)
                    {
                        await ToastService.Error("导出失败", "请选择导出时间范围");
                        return false;
                    }
                 
                    ret =await TableExport.ExportExcelAsync(await WorkOrderService.GetFlowforTime(开始时间,结束时间), context.Columns, AppContext.UserName + "_" + DateTime.Now.ToString("yyyyMMdd") + "_导出_" + ".xlsx");
                    return ret;
                },ShowSaveButton = true,
            };

            await DialogService.Show(option);

            
            return ret;
    }

     private Task OnConfirm(DateTimeRangeValue value)
    {
        开始时间 = value.Start;
        结束时间 = value.End;
        return Task.CompletedTask;
    }
      private async Task ShowToast(bool result)
    {
        if (result)
        {
            await ToastService.Success("数据导出", "导出数据成功，4 秒后自动关闭");
        }
        else
        {
            await ToastService.Error("数据导出", "导出数据失败，4 秒后自动关闭");
        }
    }
 
   private async Task<QueryData<资金流水表>> OnQueryAsync(QueryPageOptions option)=> await  WorkOrderService.QueryTable<资金流水表>(option);
       

}